/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Model;

import AirLine.Airport;
import AirLine.Booking;
import AirLine.Airline;
import Database.Airports;
import Database.Flights;
import Database.UnableToBookException;
import Database.Airlines;

import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.TreeSet;

/**
 *
 * @author smulle
 */
public class Model {

    private Airports airports = null;
    private Flights flights = null;
    private Airlines airlines = null;
    private String ERROR_MESSAGE = "";

    public Model() {

        airports = new Airports();
        airlines= new Airlines();
        flights = new Flights(airports, airlines);
        
    //myTripHandler = new TripHandler();

    }

    public Iterator getAirportsIterator() {
        Iterator retval = null;

        try {

            retval = airports.getAirportsList().iterator();

        } catch (SQLException e) {
            log(e.getMessage());

        }

        return retval;
    }

    public Airports getAirports() {
        return airports;
    }


    
    
    public Airport getAirPortByCode(String code) {
        try {
            return airports.getAirportByCode(code);
        } catch (SQLException e) {
            try {
                airports.rollBack();
                log("Couldnt get airports :");
                log(e.getMessage());
            } catch (SQLException e2) {
                log(e.getMessage());
            }
            return null;
        }
    }

        public Airline getAirlineByCode(String code) {
        try {
            return airlines.getAirlineByCode(code);
        } catch (SQLException e) {
            try {
                airlines.rollBack();
                log("Couldnt get airports :");
                log(e.getMessage());
            } catch (SQLException e2) {
                log(e.getMessage());
            }
            return null;
        }
    }
    
        public Airlines getAirlines()
    {
        return airlines;
    }
        
        public Iterator getAirlinesIterator()
        {
        Iterator retval = null;

        try {

            retval = airlines.getAirlines().iterator();

        } catch (SQLException e) {
            log(e.getMessage());

        }

        return retval;
        }
    
    public String getErrorMessage() {
        String tempString = ERROR_MESSAGE;
        ERROR_MESSAGE = "";
        return tempString;
    }

    public boolean error() {
        return (ERROR_MESSAGE.length() != 0);
    }

    public void log(String s) {
        System.out.print(s);
        ERROR_MESSAGE = ERROR_MESSAGE + " <b> " + s;
    }

    public boolean book(ArrayList<Booking> b) {

        Iterator<Booking> it = b.iterator();
        try {
            flights.connect();
            flights.setSavePoint();
        } catch (SQLException e) {
            log(e.getMessage());
            try {
                flights.closeConnections();
            } catch (SQLException e2) {
                log(e2.getMessage());
            }
            return false;
        }
        try {

            while (it.hasNext()) {
                Booking booking = it.next();

                

                flights.book(booking);
            }
            flights.commit();
        } catch (SQLException e) {
            log(e.getMessage());
            try {
                flights.rollBack();
            } catch (SQLException e2) {
                log(e2.getMessage());
            }
            return false;
        } catch (UnableToBookException e) {
            log("Unable to book :");
            log(e.getMessage());
            return false;
        }finally{
            try{
                flights.update();
                flights.closeConnections();
            }catch(SQLException e){
                log(e.getMessage());
            }
        }
        return true;
    }

    public Flights getFlights() {
        return flights;
    }

    
}
